package com.example.springbootsfzy.controller.sunkaixuan;

import com.example.springbootsfzy.domain.PrivateMessages;
import com.example.springbootsfzy.service.sunkaixuan.PrivateMessageService;
import com.example.springbootsfzy.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/PrivateMessage")
@CrossOrigin
public class PrivateMessageController {
    @Autowired
    private PrivateMessageService privateMessageService;

    //通过memberId查
    @GetMapping("/getbymemberId/{id}")
    @ResponseBody
    public R getPrivateMessages(@PathVariable int memberId) {
        List<PrivateMessages> privateMessagesList = privateMessageService.getPrivateMessages(memberId);
        return R.ok(privateMessagesList);
    }

    //通过messageId查
    @GetMapping("/getbymessageId/{id}")
    @ResponseBody
    public R getPrivateMessageById(@PathVariable int messageId) {
        PrivateMessages privateMessagesList = privateMessageService.getPrivateMessageById(messageId);
        return R.ok(privateMessagesList);
    }

    //通过consultant_id查
    @GetMapping("/selectByconsultantid/{id}")
    @ResponseBody
    public R selectByconsultantid(@PathVariable int id) {
        List<PrivateMessages> privateMessagesList = privateMessageService.selectByconsultantid(id);
        return R.ok(privateMessagesList);
    }

    /**
     * 分页查询咨询师的私信
     */
    @GetMapping("/getPrivateMessagesByConsultantId/{consultantId}")
    @ResponseBody
    public PageInfo<PrivateMessages> getPrivateMessagesByConsultantId(
                                              @RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "5") int pageSize,
                                              @PathVariable int consultantId) {
        return privateMessageService.getPrivateMessagesByConsultantId(pageNum,pageSize,consultantId);
    }

    //添加
    @PostMapping
    @ResponseBody
    public R addPrivateMessage(@RequestBody PrivateMessages privateMessages) {
        int i =  privateMessageService.addPrivateMessage(privateMessages);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }

    //修改
    @PutMapping("/updatePrivateMessage")
    @ResponseBody
    public R updatePrivateMessage(@RequestBody PrivateMessages privateMessages) {
        int i = privateMessageService.updatePrivateMessage(privateMessages);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }

    //删除
    @DeleteMapping("/deletePrivateMessage/{messageId}")
    @ResponseBody
    public R deletePrivateMessage(@PathVariable int messageId) {
        int i = privateMessageService.deletePrivateMessage(messageId);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }


}
